<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="128px">
      <el-form-item label="关联业务订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入关联业务订单编号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.EXPECT_PRICE_STATUS)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="争议状态" prop="controversyStatus">
        <el-select v-model="queryParams.controversyStatus" placeholder="请选择争议状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.CONTROVERSY_STATUS)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="出库状态" prop="outboundStatus">
        <el-select v-model="queryParams.outboundStatus" placeholder="请选择出库状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.OUTBOUND_STATUS)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="出库编号" prop="outboundNo">
        <el-input v-model="queryParams.outboundNo" placeholder="请输入出库编号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item label="确认时间" prop="confirmTime">
        <el-date-picker v-model="queryParams.confirmTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openForm(undefined)"
                         v-hasPermi="['system:order-expect-price-info:create']">新增
              </el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-edit" size="mini" @click="handleOutboundOrder"
                   v-hasPermi="['system:order-expect-price-info:outboundOrder']">批量出库
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['system:order-expect-price-info:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="table" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center"/>
      <el-table-column label="id" align="center" prop="id" width="100"/>
      <el-table-column label="关联业务订单编号" align="center" prop="orderNo"/>
      <el-table-column label="估价金额" align="center" prop="expectAmount"/>
      <el-table-column label="结算金额" align="center" prop="settlementAmount"/>
      <el-table-column label="订单状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.EXPECT_PRICE_STATUS" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="争议状态" align="center" prop="controversyStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.CONTROVERSY_STATUS" :value="scope.row.controversyStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="出库状态" align="center" prop="outboundStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.OUTBOUND_STATUS" :value="scope.row.outboundStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="出库编号" align="center" prop="outboundNo"/>
      <el-table-column label="最终确认时间" align="center" prop="confirmTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.confirmTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="争议备注" align="center" prop="remark"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"
                     v-hasPermi="['system:order-expect-price-info:update']">详情
          </el-button>
          <el-button v-if="scope.row.outboundStatus == 0" size="mini" type="text" icon="el-icon-thumb"
                     @click="outboundOrderById(scope.row.id)"
                     v-hasPermi="['system:order-expect-price-info:outboundOrderById']">出库
          </el-button>
          <el-button v-if="scope.row.status == 0 || scope.row.status == 2" size="mini" type="text" icon="el-icon-edit"
                     @click="handleControversyDialog(scope.row)"
                     v-hasPermi="['system:order-expect-price-info:updateControversy']">修改争议
          </el-button>
          <!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                               v-hasPermi="['system:order-expect-price-info:delete']">删除
                    </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!-- 对话框(添加 / 修改) -->
    <OrderExpectPriceInfoForm ref="formRef" @success="getList"/>

    <!-- 上传报告 弹框 openFormRef -->
    <el-dialog title="上传报告" :visible.sync="controversyDialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="controversyFormRef" :model="controversyFormData" :rules="controversyFormRules"
               v-loading="formLoading" label-width="130px">
        <el-form-item label="争议状态" prop="controversyStatus">
          <el-select v-model="controversyFormData.controversyStatus" placeholder="请选择状态">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.CONTROVERSY_STATUS)"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="争议备注" prop="remark">
          <el-input v-model="controversyFormData.remark" placeholder="请输入争议备注" maxlength="50"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleControversySubmit" :disabled="formLoading">确 定</el-button>
        <el-button @click="controversyDialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import * as OrderExpectPriceInfoApi from '@/api/system/orderexpectpriceinfo';
import OrderExpectPriceInfoForm from './OrderExpectPriceInfoForm.vue';
import {updateControversy} from "../../../api/system/orderexpectpriceinfo";
import {outboundOrderById} from "@/api/system/orderexpectpriceinfo";

export default {
  name: "OrderExpectPriceInfo",
  components: {
    OrderExpectPriceInfoForm,
  },
  data() {
    return {
      selectedRows: [], // 用于保存勾选的行
      controversyFormRules: {
        controversyStatus: [{required: true, message: "不能为空", trigger: "blur"}],
        remark: [{required: true, message: "不能为空", trigger: "blur"}],
      },
      formLoading: false,
      controversyDialogVisible: false,
      controversyFormData: {
        id: null,
        controversyStatus: null,
        remark: null,
      },
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单估价信息列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        orderNo: null,
        status: null,
        createTime: [],
        confirmTime: [],
        controversyStatus: null,
        outboundStatus: null,
        outboundNo: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const res = await OrderExpectPriceInfoApi.getOrderExpectPriceInfoPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除订单估价信息编号为"' + id + '"的数据项?')
      try {
        await OrderExpectPriceInfoApi.deleteOrderExpectPriceInfo(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
    async outboundOrderById(id) {
      await this.$modal.confirm('是否确认出库订单估价信息编号为"' + id + '"的数据项?')
      try {
        await OrderExpectPriceInfoApi.outboundOrderById(id);
        await this.getList();
        this.$modal.msgSuccess("出库成功");
      } catch {
        await this.getList();
      }
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有订单估价信息数据项?');
      try {
        this.exportLoading = true;
        const data = await OrderExpectPriceInfoApi.exportOrderExpectPriceInfoExcel(this.queryParams);
        this.$modal.msgSuccess("提交下载申请成功，稍后在下载记录中查看");
        // this.$download.excel(data, '订单估价信息.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },

    /** 上传报告 弹框 */
    async handleControversyDialog(row) {
      this.controversyDialogVisible = true
      this.controversyFormData = row
    },
    /** 上传报告 提交 */
    async handleControversySubmit() {
      await this.$refs["controversyFormRef"].validate();
      this.formLoading = true;
      await this.$modal.confirm('是否确认提交订单编号为"' + this.controversyFormData.orderNo + '"的数据项?')
      try {
        await OrderExpectPriceInfoApi.updateControversy(this.controversyFormData);
        await this.getList();
        this.$modal.msgSuccess("提交成功");
        this.controversyDialogVisible = false
        this.formLoading = false;
      } catch {
        this.formLoading = false;
        await this.getList();
      }
    },
    /**
     * 处理表格勾选行变化
     * @param rows 当前勾选的行数组
     */
    handleSelectionChange(rows) {
      this.selectedRows = rows;
    },
    /** 批量出库按钮操作 */
    async handleOutboundOrder() {
      if (this.selectedRows.length === 0) {
        this.$modal.msgError("请至少选择一条数据");
        return;
      }
      const selectedIds = this.selectedRows.map(row => row.id);
      console.log('勾选的ID:', selectedIds);
      // 继续调用API或执行其他逻辑
      await this.$modal.confirm('是否确认批量出库？')
      try {
        await OrderExpectPriceInfoApi.outboundOrder(selectedIds);
        await this.getList();
        this.$modal.msgSuccess("批量出库成功");
      } catch {
        await this.getList();
      }
    }


  }
};
</script>
